package com.pan.basecommon.model.http.control;

import com.pan.basecommon.constants.Constants;

import io.reactivex.Observer;
import io.reactivex.annotations.Nullable;
import io.reactivex.disposables.Disposable;

public abstract class BaseObserver<T> implements Observer<T> {

    public abstract void onSuccess(T result);

    public abstract void onFailure(Throwable e, String errorMsg);

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(@Nullable T t) {
        if (t == null) {
            onFailure(new Exception(Constants.FAIL), Constants.FAIL_REASON);
        } else {
            onSuccess(t);
        }
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onError(Throwable e) {
        onFailure(e, e.getMessage());
    }
}
